package cn.maihe.elg.operation.centers.epoint.sanming.dto;

import cn.maihe.elg.operation.centers.epoint.sanming.dto.handler.EpointGetGuaranteeInfoResp;
import cn.maihe.elg.operation.centers.epoint.sanming.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.model.dto.BaseResp;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

/**
 * @Description 漳州新点响应baseDTO
 * @Author WJH
 * @Date 2021/07/12
 */
@Getter
@Setter
public class EpointBaseRespDTO extends BaseResp {
    /**
     * 是	响应代码 0失败 1成功
     */
    private String code;
    /**
     * 是	响应信息
     */
    private String message;

    public static EpointBaseRespDTO of(EpointRespCodeEnum respCodeEnum, String message) {
        EpointBaseRespDTO respDTO = new EpointBaseRespDTO();
        respDTO.setCode(respCodeEnum.getRespCode());
        respDTO.setMessage((message == null || message.trim().length() == 0) ? respCodeEnum.getRespDesc() : message);

        return respDTO;
    }

    public static <T extends EpointBaseRespDTO> T build(EpointRespCodeEnum statusEnum, String msg, Class<T> tClass) {
        try {
            T t = tClass.newInstance();
            t.setCode(statusEnum.getRespCode());
            t.setMessage(msg);
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("初始化" + tClass.getName() + "异常：" + e.getMessage(), e);
        }
    }

    public static <T extends EpointBaseRespDTO> T success(String msg, Class<T> tClass) {
        return build(EpointRespCodeEnum.SUCCESS, msg, tClass);
    }

    public static <T extends EpointBaseRespDTO> T error(String msg, Class<T> tClass) {
        return build(EpointRespCodeEnum.FAILURE, msg, tClass);
    }

    public static EpointBaseRespDTO success(String msg) {
        return EpointBaseRespDTO.of(EpointRespCodeEnum.SUCCESS, msg);
    }

    public static EpointBaseRespDTO error(String msg) {
        return EpointBaseRespDTO.of(EpointRespCodeEnum.FAILURE, msg);
    }


    public static <T extends EpointBaseRespDTO> T error1(String msg) {
        new Object(){
            public Class getCurClass(){

                return this.getClass();
            }
        }.getCurClass();
        Class<T> aClass = (Class<T>)new EpointBaseRespDTO().getClass();
        return EpointBaseRespDTO.build(EpointRespCodeEnum.FAILURE, msg,aClass);
    }

    public static void main(String[] args) {
        EpointGetGuaranteeInfoResp resp = EpointGetGuaranteeInfoResp.error1("test");
        System.out.println("resp = " + JSON.toJSONString(resp));
    }


}
